﻿using DevExpress.EntityFrameworkCore.Security;
using DevExpress.ExpressApp.DC;
using DevExpress.ExpressApp.EFCore;
using DevExpress.ExpressApp.Security;
using Microsoft.EntityFrameworkCore;
using System.Reflection;

namespace XAF_EFCore_Sample.Module.EFCoreObjectSpaces;

public class CustomEFCoreObjectSpaceProvider<TDbContext> : SecuredEFCoreObjectSpaceProvider<TDbContext>
    where TDbContext : DbContext
{
    public CustomEFCoreObjectSpaceProvider(ISelectDataSecurityProvider selectDataSecurityProvider, IDbContextFactory<TDbContext> dbContextFactory, ITypesInfo typesInfo)
        : base(selectDataSecurityProvider, dbContextFactory, typesInfo)
    {
    }

    protected override EFCoreObjectSpace CreateObjectSpaceCore()
    {
        var selectDataSecurityProviderFieldInfo = typeof(SecuredEFCoreObjectSpaceProvider<TDbContext>).GetField("selectDataSecurityProvider", BindingFlags.Instance | BindingFlags.NonPublic);
        return new CustomEFCoreObjectSpace(TypesInfo, EntityStore, CreateDbContext, selectDataSecurityProviderFieldInfo.GetValue(this) as ISecurityStrategyBase);
    }
}
